package m.jclecai.com.view.common;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;

import m.jclecai.com.R;


/**
 * Created by andy on 2017/7/31.
 * 可以手动增加和减小的EditText;
 */

public class AddDelEditTextView extends FrameLayout {

    private EditText mEditText;
    private View mAdd;
    private View mDel;

    //最大倍数的上限值
    int maxBetNumber=50000;

    public AddDelEditTextView(Context context) {
        this(context,null);
    }

    public AddDelEditTextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public AddDelEditTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //这种表示自定义view的布局
        View.inflate(getContext(), R.layout.view_add_del_edittext,this);

        mEditText = ((EditText) findViewById(R.id.et_input_bet));
        mAdd = findViewById(R.id.view_follow_add);
        mDel = findViewById(R.id.view_follow_del);

        mAdd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //这里我们要执行的添加的逻辑
                add(1);

            }
        });
        mDel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //减少的逻辑；
                add(-1);
            }
        });

        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //这里如果高于50000自动设置为5000；
                int betNumber = getBetNumber();
                if (betNumber>maxBetNumber) {
                    mEditText.setText(maxBetNumber+"");
                    Toast.makeText(getContext(),"最多投注50000倍",Toast.LENGTH_SHORT).show();
                }

                if (mOnBetNumberChangeListener != null) {
                    mOnBetNumberChangeListener.onBetNumberChange(getBetNumber());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public interface  OnBetNumberChangeListener{
         void onBetNumberChange(int betNumber);
    }


    public void setOnBetNumberChangeListener(OnBetNumberChangeListener onBetNumberChangeListener) {
        mOnBetNumberChangeListener = onBetNumberChangeListener;
    }

    public OnBetNumberChangeListener mOnBetNumberChangeListener;



    /**
     * 增加相应的倍数；
     * @param addNumber
     */
    public void add(int addNumber) {
        int number = getBetNumber();
        number=number+addNumber;
        //上限是5万
        if(number>maxBetNumber){
            number=maxBetNumber;
        }
        if(number<0){
            number=0;
        }
        mEditText.setText(number+"");
    }

    /**
     * 设置倍数
     * @param betNumber
     */
    public void setBetNumber(int betNumber){

        if (betNumber>maxBetNumber) {
            betNumber=maxBetNumber;
        }
        if(betNumber<0){
            betNumber=0;
        }
        mEditText.setText(String.valueOf(betNumber));
    }

    /**
     * 获取倍数
     * @return
     */
    public int getBetNumber(){
        String content = mEditText.getText().toString();
        if (TextUtils.isEmpty(content)) {
            content="0";
        }
        return Integer.parseInt(content);
    }


}
